﻿#pragma once
#include <define.h>

namespace lt
{
	typedef uint8_t sigid_t;

	const sigid_t default_sigid = 0;

	class engine;

	class subscriber;

	class unsubscriber;

	enum class signal_action_type
	{
		SACT_INVALID,
		SACT_BEAR,	//高点信号
		SACT_BULL,	//低点信号
	};

	struct signal_handler
	{
		virtual void on_signal(sigid_t id, const code_t& code, signal_action_type type, double_t price) = 0;
	};

	class signal
	{

	private:

		sigid_t _id;

		engine& _engine;

		std::set<signal_handler*> _all_handler;

	public:
		/*
		 *	初始化事件
		 *	生命周期中只会回调一次
		 */
		void init(subscriber& subr);

		/*
		 *	销毁
		 */
		void destory(unsubscriber& unsubr);

		/*
		* 订阅信号
		*/
		void subscrib_signal(signal_handler* hdl);

		/*
		* 取消订阅
		*/
		void unsubscrib_signal(signal_handler* hdl);

	public:

		signal(sigid_t id, engine& engine);

		~signal();

	protected:

		/*
		 *	初始化事件
		 *	生命周期中只会回调一次
		 */
		virtual void on_init(subscriber& suber) {};

		/*
		 *	销毁
		 */
		virtual void on_destory(unsubscriber& unsuber) {};

		/*
		* 触发
		*/
		void trigger(const code_t& code, signal_action_type type, double_t price);

	};

}


